﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class BJXXSendProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForms();
            }
        }

        private void BindForms()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }

            //RMACodeLabel.Text = product.ApplicationForm.ApplicationCode;
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            contactLabel.Text = product.ApplicationForm.User.ContactPerson;
            companyLabel.Text = product.ApplicationForm.User.FullName;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            emailTxtBox.Text = product.ApplicationForm.User.UserEmail;
            shxxLbl.Text = product.ApplicationForm.BJXXSHAddress;

            UserService userservice = new UserService();
            if (product.ApplicationForm.AgentID.HasValue)
            {
                User agent = userservice.GetUserByID(product.ApplicationForm.AgentID.Value);
                Label2.Text = agent.ContactPerson;
                Label3.Text = agent.UserPhone;
                hfAgentEmail.Value = agent.UserEmail;
                applierPhoneTxtBox.Text = agent.UserMobilePhone;
            }
            else
            {
                applierPhoneTxtBox.Text = product.ApplicationForm.User.UserMobilePhone;
            }
            customerPhoneTxtBox.Text = product.ApplicationForm.User.UserMobilePhone;
            agentLabel.Text = product.ApplicationForm.ProviderName;

            //
            //Label1.Text = product.ApplicationForm.RunningNumber;
            //clientNameLabel.Text = product.ApplicationForm.User.FullName;
            spcodeLabel.Text = product.SPCode;
            productTypeLabel.Text = product.ProductType.ProductTypeName;
            productSerivalNumberLabel.Text = product.ProductSerialNumber;
            productVersionLabel.Text = product.ProductVersionNumber;
            dealWithTimeLabel.Text = product.DealWithDateTime.HasValue ? product.DealWithDateTime.Value.ToString("yyyy-MM-dd") : "";
            applyTimeLabel.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            buyTimeLabel.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            statusLabel.Text = product.Status;
            reasonLabel.Text = product.BJXXApproveComment;
            repairTupeLabel.Text = product.ProductRepairType;
            sendEmailTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            if (product.DealUserID.HasValue)
            {
                User dealuser = userservice.GetUserByID(product.DealUserID.Value);
                dealuserLabel.Text = dealuser.ContactPerson;
            }
            if (product.ApplicationForm.ApplicationType.Equals("BB"))
            {
                applyTypeLabel.Text = "备件先行";
            }
            else if (product.ApplicationForm.ApplicationType.Equals("BD"))
            {
                applyTypeLabel.Text = "DOA";
            }
            // sp product 
            requireDisPLabel.Text = product.BJSQNeedComment;
            spProductTypeTextBox.Text = product.ProductType.ProductTypeName;
            spSerialNumberTextBox.Text = product.ProductSerialNumber;
            spVersionTextBox.Text = product.ProductVersionNumber;

            // for buttons
            //if (product.NoticeClientDateTime.HasValue)
            //{
            //    sendLinkButton.Enabled = true;
            //}
            //else
            //{
            //    sendLinkButton.Enabled = false;
            //}


            // bind last send product information
            if (product.ApplicationForm.ParentBJXXNO.HasValue)
            {
                ApplicationService appservice = new ApplicationService();
                ApplicationForm parentForm = appservice.GetApplicationFormByID(product.ApplicationForm.ParentBJXXNO.Value);
                if (parentForm!=null&&!parentForm.ApplicationProduct.IsLoaded)
                {
                    parentForm.ApplicationProduct.Load();
                }

                if (parentForm.ApplicationProduct.Count > 0)
                {
                    lastproductserialnumberTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductSerialNumber;
                    lastproducttypeTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductType;
                    lastproductVersionTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductVersion;
                    lastsendRemarkTextBox.Text = product.Remark;
                }

                reSendPanel.Visible = true;
            }

            

            emailNoticeTxtBox.Text = string.Format(WebConfigurationManager.AppSettings["BJXXSendProductEmailNoticeBody"], product.ApplicationForm.User.ContactPerson,
                product.ApplicationForm.ApplicationCode, product.ProductType.ProductTypeName, DateTime.Now.Month, DateTime.Now.Day);
            SMS2ApplierTxtBox.Text = string.Format(WebConfigurationManager.AppSettings["BJXXSendProductSMS2ApplierBody"],
                product.ApplicationForm.ApplicationCode, product.ProductType.ProductTypeName, DateTime.Now.Month, DateTime.Now.Day);
            SMSToCustomerTxtBox.Text = string.Format(WebConfigurationManager.AppSettings["BJXXSendProductSMS2CustomerBody"],
                product.ApplicationForm.ApplicationCode, product.ProductType.ProductTypeName, DateTime.Now.Month, DateTime.Now.Day);
            SMSToSaleTxtBox.Text = string.Format(WebConfigurationManager.AppSettings["BJXXSendProductSMS2SaleBody"], product.ApplicationForm.User.CompanyName,applyTypeLabel.Text,
                product.ApplicationForm.ApplicationCode, product.ProductType.ProductTypeName, DateTime.Now.Month, DateTime.Now.Day);
        }

       

        protected void saveSendRecordButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(spSerialNumberTextBox.Text))
            {
                ApplicationProductService service = new ApplicationProductService();
                ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
                product.Status = "备件发货完毕待客户收货确认";
                product.BJXXSendToClient = Convert.ToDateTime(sendEmailTextBox.Text);
                product.ReplaceProductSerialNumber = spSerialNumberTextBox.Text;
                product.ReplaceProductType = spProductTypeTextBox.Text;
                product.ReplaceProductVersion = spVersionTextBox.Text;
                product.Remark += "备件发货："+spRemarkTextBox.Text+DateTime.Now.ToString();

                service.UpdateProduct(product);

                //string body = WebConfigurationManager.AppSettings["BJXXSendProduct"];
                //string emailBody = string.Format(body, contactLabel.Text, spProductTypeTextBox.Text);

                //EmailService.SendEmail(emailLabel.Text, "备件先行发货", emailBody);

                //if (!string.IsNullOrEmpty(hfAgentEmail.Value))
                //{
                //    EmailService.SendEmail(hfAgentEmail.Value, "备件先行发货", emailBody);
                //}

                if (isSendEmailChkBox.Checked)
                {
                    string[] emails = emailTxtBox.Text.Trim().Split(new char[] { '，', '，', ' ', ';', });
                    for (int index = 0; index < emails.Length; index++)
                    {
                        EmailService.SendEmail(emails[index], "备件先行发货", emailNoticeTxtBox.Text.Trim());
                    }
                }
                if (isSendSMS2ApplierChkBox.Checked)
                {
                    SMSService.SendSMS(SMS2ApplierTxtBox.Text.Trim(), applierPhoneTxtBox.Text.Trim());
                }
                if (isSendSMS2CustomerChkBox.Checked)
                {
                    SMSService.SendSMS(SMSToCustomerTxtBox.Text.Trim(), customerPhoneTxtBox.Text.Trim());
                }
                if (isSendSMS2SaleChkBox.Checked)
                {
                    string[] saleMobilePhone = salePhoneTxtBox.Text.Trim().Split(new char[] { '，', '，', ' ', ';', });
                    for (int index = 0; index < saleMobilePhone.Length; index++)
                    {
                        SMSService.SendSMS(SMSToSaleTxtBox.Text.Trim(), saleMobilePhone[index]);
                    }
                }
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('备件发货完毕待客户收货确认');location.href='BJXXListNeedSend.aspx';", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请输入发送的序列号');", true);
            }
        }

        //protected void emailLinkButton_Click(object sender, EventArgs e)
        //{
        //    //if (!string.IsNullOrEmpty(sendSerialNumberTextBox.Text))
        //    //{
        //    Modalpopupextender1.Show();
        //    string body = WebConfigurationManager.AppSettings["BJXXSendProduct"];
        //    bodyTextBox.Text = string.Format(body, contactLabel.Text, spProductTypeTextBox.Text);
        //    //}
        //    //else
        //    //{
        //    //    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请输入发送的序列号');", true);
        //    //}

        //}

        //protected void sendMailButton_Click(object sender, EventArgs e)
        //{
        //    ApplicationProductService service = new ApplicationProductService();
        //    ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
        //    product.NoticeClientDateTime = DateTime.Now;
        //    service.UpdateProduct(product);

        //    EmailService.SendEmail(emailLabel.Text, "备件先行发货", bodyTextBox.Text);

        //    if (!string.IsNullOrEmpty(hfAgentEmail.Value))
        //    {
        //        EmailService.SendEmail(emailLabel.Text, "备件先行发货", bodyTextBox.Text);
        //    }

        //    BindForms();
        //}

        protected void sendLinkButton_Click(object sender, EventArgs e)
        {
            Modalpopupextender2.Show();
        }

    }
}
